const axios = require('axios')
const cheerio = require('cheerio')
const jieba = require('nodejieba')

async function fetchSingleDoubanList(start){
    let res = await axios.get(`https://www.douban.com/group/shanghaizufang/discussion?start=${start}`)
    let htmlText = res.data
    // console.log(htmlText)
    /**
     * 拉到html文档，又不是在浏览器上解析，所以没有浏览器上下文环境 cheerio提供了一种轻量级的解决方案
     */
    const $ = cheerio.load(htmlText)
    const rs = $('a[title]')  // 把有title属性的a标签提取出来
    // console.log(rs)

    /**
     * resultList获取带有帖子标题的字和url
     */
    const resultList = []

    for(let i=0; i<rs.length; i++){
        resultList.push({
            title: rs.eq(i).attr('title'),
            url: rs.eq(i).attr('href')
        })
    }

    // console.log(resultList)
    // console.log(resultList.length)
    
    return resultList
}

async function fetchSingleDoubanTopic(url){
    let res = await axios.get(url)
    let htmlText = res.data
    const $ = cheerio.load(htmlText)
    const ps = $('.topic-richtext > p')
    let details = []
    for(let i = 0; i < ps.length; i++){
        details.push(ps.eq(i).text())
    }
    const topicPics = $('.image-wrapper > img')

    let pics = []
    for(let i=0; i<topicPics.length; i++){
        pics.push(topicPics.eq(i).attr('src'))
    }

    return {
        details,
        pics
    }



    // console.log(topicBody)

}

module.exports = {
    fetchSingleDoubanList,
    fetchSingleDoubanTopic
}